//
//  PostWorkOrderViewController.m
//  NewPro
//
//  Created by RYX on 2018/11/23.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "PostWorkOrderViewController.h"
#import "WYIDScanViewController.h"
#import "WYScanResultModel.h"
#import "WYRectManager.h"
#import "PostWorkOrderDataParameters.h"
#import "WorkOrderList.h"
#import "WorkOrderModel.h"
#import "WorkOrderStore.h"
#import "PostWorkOrderSuccessViewController.h"

@interface PostWorkOrderViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    
    __weak IBOutlet NSLayoutConstraint *topMargin;
    __weak IBOutlet UIScrollView *sView;
    __weak IBOutlet UIView *payModelView;
    
    __weak IBOutlet UIImageView *modelImView;
    
}

@property (nonatomic,copy) NSString *imgUrl;

@property (nonatomic,weak) UIButton *currentBtn;
@property (nonatomic,strong) PostWorkOrderDataParameters *parameters;

@end

@implementation PostWorkOrderViewController

- (PostWorkOrderDataParameters *)parameters{
    
    if (!_parameters) {
        
        _parameters = [[PostWorkOrderDataParameters alloc] init];
        _parameters.customId = [UserDefaultsManager sharedInstance].user_id;
        _parameters.workOrderId = [WorkOrderList sharedInstance].selectedModel.uid;
    }
    
    return _parameters;
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:YES];
    self.currentBtn.enabled = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imgUrl = @"";
    
    payModelView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
    
    [self setNavBarWithTitle:@"工单资料提交"];
    
    [sView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.right.bottom.equalTo(self.view);
        
    }];
    
}

#pragma mark - 上传身份证正面
- (IBAction)onScanIdFontBtn:(UIButton *)sender {
    
    sender.enabled = NO;
    self.currentBtn = sender;
    
    WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDFront];
    [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
        
        sender.enabled = YES;
        [sender setImage:scanModel.idImage forState:UIControlStateNormal];
        
        self.parameters.idCardFront = [ImgManager imageToString:scanModel.idImage];
        
        
    }];
    
    [VC scanFailure:^{
        
        sender.enabled = YES;
        [SVPManager showErrorWithTitle:@"请扫描身份证正面"];
    }];
    
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 上传身份证背面
- (IBAction)onScanIdBackBtn:(UIButton *)sender {
    
    sender.enabled = NO;
    self.currentBtn = sender;
    
    WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDDown];
    
    [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
        
        sender.enabled = YES;
        
        [sender setImage:scanModel.idImage forState:UIControlStateNormal];
        
        self.parameters.idCardBack = [ImgManager imageToString:scanModel.idImage];
        
    }];
    
    [VC scanFailure:^{
        
        sender.enabled = YES;
        [SVPManager showErrorWithTitle:@"请扫描身份证反面"];
    }];
    
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 上传手持身份证照片
- (IBAction)onHandIdBtn:(UIButton *)sender {
    
    sender.tag = 1000;
    
    [self takePhotoWithBtn:sender];

}

#pragma mark - 上传银行卡正面
- (IBAction)onScanBankCardFontBtn:(UIButton *)sender {
    
    sender.enabled = NO;
    self.currentBtn = sender;
    
    WYIDScanViewController *VC = [[WYIDScanViewController alloc] initWithCarInfo: CardIDBank];
    
    [VC scanDidFinishCarInfo:^(CardType status, WYScanResultModel *scanModel) {
        
        sender.enabled = YES;
        
        [sender setImage:scanModel.bankImage forState:UIControlStateNormal];
        
        self.parameters.bankFront = [ImgManager imageToString:scanModel.bankImage];
    }];
    
    [VC scanFailure:^{
        
        sender.enabled = YES;
    }];
    
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark - 上传银行卡背面
- (IBAction)onTakePhotoOfBankCardBackBtn:(UIButton *)sender {
    
    sender.tag = 1001;
    [self takePhotoWithBtn:sender];
    
}

#pragma mark - 上传手持银行卡照片
- (IBAction)onHandBankCardBtn:(UIButton *)sender {
    
    sender.tag = 1002;
    [self takePhotoWithBtn:sender];
    
}

#pragma mark - 查看支付证明模板
- (IBAction)onLookPayModelBtn:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    
    if (sender.selected) {
        
        if (self.imgUrl.length <= 0) {
            
            
            WorkOrderStore *store = [[WorkOrderStore alloc] init];
            
            MJWeakSelf
            [store getConsumptionURLSuccess:^(NSString *imgStr) {
                
                self.imgUrl = imgStr;
                
                [self->modelImView sd_setImageWithURL:[NSURL URLWithString:self.imgUrl]];
                
                [UIView animateWithDuration:0.25 animations:^{
                    
                    self->payModelView.alpha = 1;
                    self->payModelView.frame = CGRectMake(0, 0, ScreenWidth, 577);
                    
                } completion:^(BOOL finished) {
                    self->modelImView.width = self->payModelView.width - 14 * 2;
                    self->modelImView.height = self->payModelView.height - 43 * 2;
                }];
                
            } Failure:^(NSError *error) {
                
                [weakSelf showMBPError:[HttpTool handleError:error]];
            }];
            
        }else{
        
            [self->modelImView sd_setImageWithURL:[NSURL URLWithString:self.imgUrl]];
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self->payModelView.alpha = 1;
                self->payModelView.frame = CGRectMake(0, 0, ScreenWidth, 577);
                
            } completion:^(BOOL finished) {
                self->modelImView.width = self->payModelView.width - 14 * 2;
                self->modelImView.height = self->payModelView.height - 43 * 2;
            }];
            
        }
        
        
    }else{
        
        [UIView animateWithDuration:0.25 animations:^{
            
            self->payModelView.alpha = 0;
            self->payModelView.frame = CGRectMake(ScreenWidth - 200, 0, 100, 50);
            
        } completion:^(BOOL finished) {
            self->modelImView.width = self->payModelView.width - 14 * 2;
            self->modelImView.height = self->payModelView.height - 43 * 2;
        }];
    }
    
}

#pragma mark - 上传支付证明照片
- (IBAction)onPayWarrantBtn:(UIButton *)sender {
    
    sender.tag = 1003;
    [self takePhotoWithBtn:sender];
    
}

#pragma mark - 上传手持支付证明照片
- (IBAction)onHandPayWarrantBtn:(UIButton *)sender {
    
    sender.tag = 1004;
    [self takePhotoWithBtn:sender];
    
}

#pragma mark - 上传支付小票照片
- (IBAction)onPayTicketBtn:(UIButton *)sender {
    
    sender.tag = 1005;
    [self takePhotoWithBtn:sender];
    
}


- (void)takePhotoWithBtn:(UIButton *)sender{
    
    self.currentBtn = sender;
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        return [self showMBPError:@"未发现摄像头"];
        
    }
    
    [self takePhoto];
}

- (void)takePhoto{
    
    UIImagePickerController * picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//    picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
    [self presentViewController:picker animated:YES completion:nil];
    
}
#pragma mark - 照片选择完成
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];

        NSLog(@"当前按钮tag:%ld",(long)self.currentBtn.tag);
        
        NSInteger tag = self.currentBtn.tag;
        
        [self setParametersWithImg:image andTag:tag];
        [self.currentBtn setImage:image forState:UIControlStateNormal];
        
    }];
}

- (void)setParametersWithImg:(UIImage *)img andTag:(NSInteger)tag{
    
    if (tag == 1000) {
        
        self.parameters.holdIdCardFront = [ImgManager imageToString:img];
    }
    
    if (tag == 1001) {
        
        self.parameters.bankBack = [ImgManager imageToString:img];
    }
    
    if (tag == 1002) {
        
        self.parameters.holdBankFront = [ImgManager imageToString:img];
    }
    
    if (tag == 1003) {
        
        self.parameters.payment = [ImgManager imageToString:img];
    }
    
    if (tag == 1004) {
        
        self.parameters.holdPayment = [ImgManager imageToString:img];
    }
    
    if (tag == 1005) {
        
        self.parameters.payableBills = [ImgManager imageToString:img];
    }

}


#pragma mark - 提交工单
- (IBAction)onPostBtn:(UIButton *)sender {
    
    if (self.parameters.idCardFront.length <= 0) {
        
        return [self showMBPError:@"请上传身份证正面照片"];
    }
    
    if (self.parameters.idCardBack.length <= 0) {
        
        return [self showMBPError:@"请上传身份证背面照片"];
    }
    
    if (self.parameters.holdIdCardFront.length <= 0) {
        
        return [self showMBPError:@"请上传手持身份证正面照片"];
    }
    
    if (self.parameters.bankFront.length <= 0) {
        
        return [self showMBPError:@"请上传交易卡正面照片"];
    }
    
    if (self.parameters.bankBack.length <= 0) {
        
        return [self showMBPError:@"请上传交易卡背面照片"];
    }
    
    if (self.parameters.holdBankFront.length <= 0) {
        
        return [self showMBPError:@"请上传手持交易卡正面照片"];
    }
    
    if (self.parameters.payment.length <= 0) {
        
        return [self showMBPError:@"请上传支付证明照片"];
    }
    
    if (self.parameters.holdPayment.length <= 0) {
        
        return [self showMBPError:@"请上传手持支付证明照片"];
    }
    
    [self.view makeToastActivity:CSToastPositionCenter];
    
    WorkOrderStore *store = [[WorkOrderStore alloc] init];
    MJWeakSelf
    [store postWorkOrderDataWithWorkWithParameters:self.parameters Success:^{
        
        [weakSelf.view hideToastActivity];
        
        PostWorkOrderSuccessViewController *VC = [[PostWorkOrderSuccessViewController alloc] init];
        [weakSelf.navigationController pushViewController:VC animated:YES];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"POSTWORKORDERSUCCESS" object:nil];
        
    } Failure:^(NSError *error) {
       
        [weakSelf.view hideToastActivity];
        [weakSelf showMBPError:[HttpTool handleError:error]];
        
    }];
}
@end
